package org.ucas.faker.web.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.ucas.faker.web.entity.TaskLog;

import java.util.List;

import static org.ucas.faker.web.entity.TaskLog.TaskCategory.WX_SEARCH;

@Repository
public interface TaskDao extends PagingAndSortingRepository<TaskLog,Long> {

  public TaskLog findByDepotIdAndVersionAndCategory(long depotId, long version, TaskLog.TaskCategory category);

  @Query("SELECT t FROM TaskLog t WHERE t.depotId = ?1 AND t.category = ?2 ORDER BY t.version DESC ")
  public List<TaskLog> history(long depotId, TaskLog.TaskCategory category);

  @Query("SELECT t FROM TaskLog t WHERE t.depotId = ?1 AND t.category = ?2 ORDER BY t.version DESC ")
  public Page<TaskLog> history(long depotId, TaskLog.TaskCategory category, Pageable page);

  @Query("SELECT t.attachment FROM TaskLog t WHERE t.operator = ?1 AND t.category = 3 ORDER BY t.createAt DESC")
  public Page<String> queries(long userId, Pageable page);

  @Modifying
  @Query("UPDATE TaskLog t SET t.status = ?2 WHERE t.id = ?1")
  public void updateStatus(long taskId, TaskLog.TaskStatus status);

  @Modifying
  @Query("UPDATE TaskLog t SET t.status = ?2, t.progress = ?3 WHERE t.id = ?1")
  public void updateStatus(long taskId, TaskLog.TaskStatus status, Integer progress);

  @Query("SELECT t.status FROM TaskLog t WHERE t.id = ?1")
  public TaskLog.TaskStatus queryStatus(long taskId);

}
